package com.zzf.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.zzf.mall.order.vo.PayVO;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * 支付宝的配置文件
 *
 * @Author 赵振飞
 * @Date 2022/11/7
 * @Version 1.0
 */
@Component
@Data
public class AlipayTemplate {
    // 商户appid
    public static String APPID = "2021000121687201";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCWCq/CiZF21b1qbmPzL3ZhwhyY2GKLXh9ep1XyXB8OSg0msmQuq+TWpmxHEMyPPGrY0NmNdDpRQNriSUlYp4EDcXyv0edgsZCGTkOqvtxo7ycQga6m8Wk9r9zrBsNPEveik7hSQMVxh+J/EDX+B4Q9GD9soqsM42gX5gqd/wRImOAhIxMC1FEJkPhKm1VcSDr+tx3+nK7XJSTZpEL6ZaYlY/GL/EWI4aBoqeeT9I0HYbfERhvf32nSMWSSYKKSb8ZgkTMfgTWRBgTJRPABv+DbY+/i68OG8C6wDOHTC0B3KjEBxatnbOpIceRLtji0sip9GsI8PMQsmFInbQvTC+ZVAgMBAAECggEAIrgUVaK5q1LoQsGKlVJNRW/CaEfh/bfo8GEuTSet9AOEqYisD/SsvlLCSWuEujxTyRoCzW2wtO6Z97TXqRERlHdlT9X1gXV1KO7hT5hcrldwpawseurv3FtednAZL6oG7XLbOwVL6upzqc1ZgQ/CNNTwkDXP+OqCQs1zdX32QsnpbXEAKFWsL84knfyeYQoIXOsA0KJmaGSsrSjSFYOiN5NsDpeUaoAxwCwX2ZNJbVJ7O3ptiQSRrr4nwUXhfYhH227icqG+tYyBCd8kocO7IJfWGiHuukatOjoL9M8RiGUPoJ0zoKRMBx6CThccRaofAk0i+vNnrtXaHKRbHZ72oQKBgQDl/qlVb7C5vXd4uGqRO7AeGQEN8rvQR7WTA56fxTXbNTAo9+gUqXkzXtmzCBP5Qe0tZ/AMbGhD2pmtlzND7uNSjnvuSu0mIu+4zqDpNnqINO5c44yay1SIr/HFFBP5S4c/3y8XNHA9OPgAszYToUPQnt3X+s+xgEIgRTK77DBlOQKBgQCnAbxxrBeLyxKe4hE57bU+HrH3EZSeBx7cTQH0D94cN3aFLCYZ+xDGkZ+WcdyCRbfrguo5ddO2NbelKhzL61jogbvnKn/QcTCJa2K9QxX/OepLX0FMjP4oRcThlKDGLNM0Swmz/Rqt2MoiULc0VxEFil7s69mSumxcR8adLuvF/QKBgEvEh1hkAR5PcopMhp7twOlJZ3WG36SP2xthd+I+f62FaiY09TnJUuo/dwxpvtrAgzgQI56UIA0qeLIdO4BHtI+/7Mfl2xZTMBH70DrD/b3dYiErvWhJT8Hv5H/+S043OYkrJ0JHvioNGHZyk4AIAmU50O+T6tQY4q/q2/nYp2DRAoGAE4ewcdqeJkrpMotdWFjkCQ+t4Ca+KnzKrxiSUehJISObSJeRHRivLYl6EC3gi5t7lp8IpQ+SYoyFnVr/YxlynD4Pq0jdCZMzCd70/Q0LVX2AWLAbAsi6MBt4mqi25HX5fPQJfnLq28muWbT2t7g21V8JGsu59d63LVN3doo2KEECgYEAjmMWbxWWMRDBlmOva1W+tOh8RW8gJk8A0Kuk0aXX6WWoDo5IvuFwXx7Ud3Xs4kRHqhRq4aoyQF8JCmgCrH1eielFtztovV1+WmUSXhJdVIof0EBlShTB3H9khHQjZdD1MUd+2WRKu1Ss01K6rkXjCQIF2dRMMicvhhd/bMtTHTk=";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://localhost:8080/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url = "http://order.zzf.com/orderPay/returnUrl";
    // 请求网关地址
    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnusDEZkpnYWyVDjr0GRLt0CXD5p11i65e58yd1QDGKdzzKrXYOSEu4rzrGG9UgKLqhRtqYcCfaoB/X9wzvPzSDGNmz808XVXRB/ANf+hwxNWVsWfjluEm96gsvH2yxe4cEGOyIFa2yByFyMNv+cf0nl0To/YMvoC8RTYuIXIu7o6bWeTvrEF5EctK64lP5aQM4/BIFw6M2NBkdIP39yUtOgT8Q1LPaq2SFFZc4YAxhIFR7nH1RI2X5rBYlEfMUvgMtOGkpthiQMuoWyxQnJL7ml7+OU0RmZu14QsTwC/vwYSJHzhTyiOi/ZBkra5xVCXM8r2/AGqUSLSjiwv1UE6NwIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";


    public String pay(PayVO payVO){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL,
                APPID,
                RSA_PRIVATE_KEY,
                FORMAT,
                CHARSET,
                ALIPAY_PUBLIC_KEY,
                SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVO.getOut_trader_no());
        model.setSubject(payVO.getSubject());
        model.setTotalAmount(payVO.getTotal_amount());
        model.setBody(payVO.getBody());
        model.setTimeoutExpress("5000");
        model.setProductCode("11111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  null;
    }
}
